package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelContdition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.uitl.newException.LogiclException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(Travel travel) {
        travel.setId(null);
        travelRepository.save(travel);
    }

    @Override
    public void delete(String id) {
        travelRepository.deleteById(id);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public Travel get(String id) {
        Travel travel = travelRepository.findById(id).orElse(null);
        //一定用大对象和目的地放与
        travel.setAuthor(userInfoService.get(travel.getUserId()));
        travel.setDest(destinationService.get(travel.getDestId()));
        return travel;
    }

    @Override
    public List<Travel> lists() {
        return travelRepository.findAll();
    }
    //分页查询
    @Override
    public Page<Travel> query(TravelQuery qo) {
        //创建查询对象
        Query query = new Query();
        //条件
        if (qo.getState()==1){
            query.addCriteria(Criteria.where("state").is(Travel.STATE_WAITING));
        }else if (qo.getState()==-1){
            query.addCriteria(Criteria.where("state").is(Travel.STATE_REJECT));
        }
        //条件
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //天数条件
        TravelContdition day = qo.getDay();
        if (day !=null){
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }
        //人均消费
        TravelContdition perexpend = qo.getPerexpend();
        if (perexpend !=null){
            query.addCriteria(Criteria.where("perExpend").gte(perexpend.getMin()).lte(perexpend
                    .getMax()));
        }
        //通过查询拿到总条数
        long count = mongoTemplate.count(query, Travel.class);
        //判断有没有数据
        if (count==0){
            return Page.empty();//返回一个空,页面不会错,如果直接返回一个0页面就会报错
        }
        //最新最热
        Pageable request =null;
        if (qo.getOrderType()==1){
            request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "lastUpdateTime");
        }else if (qo.getOrderType()==2){
            request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "thumbsupnum");
        }else {
            //无条件分页
            request = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        }


        //查询所有数据
        query.with(request);
        List<Travel> list = mongoTemplate.find(query, Travel.class);
        PageImpl<Travel> page = new PageImpl<>(list, request, count);
        //维护用户关系
        for (Travel travel : page) {
            UserInfo userInfo = userInfoService.get(travel.getUserId());
            travel.setAuthor(userInfo);
        }
        return page;
    }

    /**
     * 添加和删改
     * @param travel
     */
    @Override
    public void saveOUpdate(Travel travel) {
        if (StringUtils.hasLength(travel.getId())){
            //1先查询  2再替换  3修改
            //这个是特殊的类,
            this.update(travel);
        }else {
            this.save(travel);
        }
    }
    //审核功能
    @Override
    public void changeState(String id, int state) {
        //什么条件下审核
        Travel travel = this.get(id);
        if (travel==null){
            throw new LogiclException("参数有误");
        }
        if (state==Travel.STATE_NORMAL){
            throw new LogiclException("还没有到审核这一步");
        }

        if (state == Travel.STATE_RELEASE) {
                //审核通过时做什么
                //更新时间
                travel.setLastUpdateTime(new Date());
                //修改状态
                travel.setState(state);
                //发布时间
                travel.setReleaseTime(new Date());
                //存档
                this.update(travel);
        } else if (state == Travel.STATE_REJECT) {
                //审核通过时做什么
                //更新时间
                travel.setLastUpdateTime(new Date());
                //修改状态
                travel.setState(state);
                //发布时间
                travel.setReleaseTime(null);
                this.update(travel);
        } else if (state == Travel.STATE_WAITING) {
            //审核通过时做什么
                //更新时间
                travel.setLastUpdateTime(new Date());
                //修改状态
                travel.setState(state);
                //发布时间
                travel.setReleaseTime(null);
                this.update(travel);
            }
        }
    //修改/添加中的提交功能
    @Override
    public void saveOrUpdate(Travel travel) {
        //有一个目的地名字
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());
        //最后更新时间
        travel.setLastUpdateTime(new Date());
        if (StringUtils.hasLength(travel.getId())){
            this.update(travel);
        }else {
            //发布时间
            travel.setReleaseTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public List<Travel> queryfindByDestName(String keyword) {
        List<Travel> travels = travelRepository.findByDestName(keyword);
        for (Travel travel : travels) {
            //一定用大对象和目的地放与
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travels;
    }


}
